<template>
	<view>
		<view class="p-30">
			<u-form labelPosition="left" :model="form" :rules="rules" ref="form" labelWidth="0" errorType="toast">
				<view class="card p-30">
					<u-form-item label=" " prop="username">
						<view class="flex-row justify-between align-center w-full">
							<view class="label">
								选择
							</view>
							<view class="w-full">
								<u-radio-group v-model="radiovalue1" placement="row">
									<u-radio :customStyle="{marginRight: '30rpx'}" v-for="(item, index) in radiolist1"
										:key="index" :label="item.name" :name="item.name">
									</u-radio>
								</u-radio-group>
							</view>
						</view>
					</u-form-item>
					<u-form-item label=" " prop="username">
						<view class="flex-row justify-between align-center w-full">
							<view class="label">
								收发货人
							</view>
							<view class="w-full">
								<u-input v-model="form.username" border="none"></u-input>
							</view>
						</view>
					</u-form-item>
					<u-line color="#F6F6F6"></u-line>
					<u-form-item label=" " prop="mobile">
						<view class="flex-row justify-between align-center w-full">
							<view class="label">
								联系方式
							</view>
							<view class="w-full">
								<u-input type="number" v-model="form.mobile" border="none"></u-input>
							</view>
						</view>
					</u-form-item>
					<u-line color="#F6F6F6"></u-line>
					<u-form-item label=" " prop="mobile">
						<view class="flex-row justify-between align-center w-full">
							<view class="label">
								地址
							</view>
							<view class="w-full">
								<uni-section title="本地数据" type="line">
									<view class="uni-px-5 uni-pb-5">
										<uni-data-picker placeholder="请选择地区" popup-title="请选择"
											:localdata="classDataTree" v-model="classValue" @change="onchange"
											@nodeclick="onnodeclick" @popupopened="onpopupopened"
											@popupclosed="onpopupclosed"></uni-data-picker>
									</view>
								</uni-section>
							</view>

						</view>
					</u-form-item>
					<u-form-item label=" " prop="mobile">
						<view class="flex-row justify-between  w-full">
							<view class="label">
								详细地址
							</view>
							<view class="w-full">
								<!-- <u-input type="tex" v-model="form.address" border="none"></u-input> -->
								<u-textarea v-model="form.address" placeholder="请输入详细地址" border="none"></u-textarea>
							</view>
						</view>
					</u-form-item>
				</view>
			</u-form>
			<view class="mt-30" @click="submit">
				<u-button :loading="loading" type="primary" color="#FC3F39" shape="circle" text="保存地址"></u-button>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		addressAdd,
		addressInfo
	} from '@/common/api.js'
	export default {
		data() {
			return {
				classValue: '1-2',
				classDataTree: [{
						text: '一年级',
						value: '1-0',
						children: [{
								text: '1.1班',
								value: '1-1'
							},
							{
								text: '1.2班',
								value: '1-2'
							}
						]
					},
					{
						text: '二年级',
						value: '2-0',
						children: [{
								text: '2.1班',
								value: '2-1'
							},
							{
								text: '2.2班',
								value: '2-2'
							}
						]
					},
					{
						text: '三年级',
						value: '3-0',
						disable: true
					}
				],
				radiolist1: [{
						name: '国内',
						disabled: false
					},
					{
						name: '海外',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				radiovalue1: '国内',
				loading: false,
				form: {
					username: '',
					mobile: "",
					address: ""
				},
				relation: '',
				rules: {
					username: {
						type: 'string',
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					},
					mobile: [{
							required: true,
							message: '请输入联系方式',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '联系方式不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					address: {
						type: 'string',
						required: true,
						message: '请输入详细地址',
						trigger: ['blur', 'change']
					},
				}
			};
		},
		onLoad(e) {
			if (e.id) this.getInfo(e.id);
		},
		methods: {
			onnodeclick(e) {
				console.log('nodeclick', e);
			},
			onpopupopened(e) {
				console.log('onpopupopened');
			},
			onpopupclosed(e) {
				console.log('onpopupclosed');
			},
			onchange(e) {
				console.log('onchange', e.detail.value);
			},
			getInfo(id) {
				addressInfo({
					data: {
						id
					}
				}).then(res => {
					this.form = res;
					console.log('this.form', this.form);
				})
			},
			submit() {
				let vm = this;
				this.loading = true;
				console.log(this.form);
				this.$refs.form.validate().then(res => {
					addressAdd(this.form).then(res => {
						console.log(res);
						vm.$refs.uToast.show({
							type: 'success',
							message: '保存成功',
							complete() {
								vm.loading = false;
								uni.redirectTo({
									url: '/pages/mine/index'
								})
							}
						})
					})
				}).catch(errors => {
					// uni.$u.toast('校验失败')
					vm.loading = false;
				})
			},
			//地址
			setAdress() {
				uni.chooseLocation({
					// latitude: latitude || '',
					// longitude: longitude || '',
					success: (res) => {
						console.log('wx.chooseLocation res=', res)
						this.form.address = res.address
					},
					fail: function(err) {
						console.log('取消按钮', err)
					}
				})
			},
			onReady() {
				// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
				this.$refs.form.setRules(this.rules);
			}
		}

	};
</script>


<style>
	.title {
		font-size: 14px;
		font-weight: bold;
		margin: 20px 0 5px 0;
	}

	.data-pickerview {
		height: 400px;
		border: 1px #e5e5e5 solid;
	}

	.popper__arrow {
		top: -6px;
		left: 50%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #EBEEF5;
	}

	.popper__arrow {
		top: -6px;
		left: 50%;
		margin-right: 3px;
		border-top-width: 0;
		border-bottom-color: #EBEEF5;
	}

	.label {
		width: 250rpx;
	}
</style>